package org.jeecg.modules.charge.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.CacheConstant;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.vo.charge.ChargeMethod;
import org.jeecg.modules.charge.mapper.ChargeMethodMapper;
import org.jeecg.modules.charge.service.IChargeMethodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @Description: 收款方式
 * @Author: jeecg-boot
 * @Date:   2019-07-04
 * @Version: V1.0
 */
@Service
public class ChargeMethodServiceImpl extends ServiceImpl<ChargeMethodMapper, ChargeMethod> implements IChargeMethodService {

    @Autowired
    private  ChargeMethodMapper chargeMethodMapper;

    @Override
    public Page<ChargeMethod> listByParams(Page<ChargeMethod> page, String name,String tenantId) {
        return page.setRecords(chargeMethodMapper.listByParams(page,name,tenantId));
    }

    @Override
    public Result add(ChargeMethod chargeMethod) {
        int outLimit = chargeMethodMapper.checkOutLimit(chargeMethod.getTenantId());
        if (outLimit >= 10){
            return Result.error("自定义收款方式已超上限10个,添加失败！");
        }
        //判断方式名是否存在
        int existFlg = chargeMethodMapper.findByName(chargeMethod.getTenantId(),chargeMethod.getMethodName());
        if (existFlg > 0 ){
            return Result.error("收款方式已存在，请勿重复添加！");
        }
        try{
            //自动生成 payTypeField
            String payTypeFieldName = "";
            ChargeMethod temp = new ChargeMethod();
            temp.setTenantId(chargeMethod.getTenantId());
            temp.setIsDefault(false);
            List<ChargeMethod> existList =  chargeMethodMapper.listByCondition(temp);
            if (existList == null || existList.size() <= 0 ){
                payTypeFieldName = CommonConstant.CHARGE_METHOD_FIELD_NAME_PREFIX + "0";
            } else {
                Set<String> existSet = existList.stream().map(ChargeMethod::getMethodField).collect(Collectors.toSet());
                for (int i=0;i<10;i++){
                    String fieldName = CommonConstant.CHARGE_METHOD_FIELD_NAME_PREFIX + i;
                    if (!existSet.contains(fieldName)){
                        payTypeFieldName = fieldName;
                        break;
                    }
                }
            }
            chargeMethod.setIsDefault(false);
            chargeMethod.setMethodField(payTypeFieldName);
            chargeMethodMapper.insert(chargeMethod);
        } catch (Exception e){
            log.error("收款方式添加异常,{}",e);
            return Result.error("收款方式添加异常");
        }
        return Result.ok("添加成功");
    }

    @Override
    @Cacheable(value = CacheConstant.PAY_METHOD,key = "#tenantId+#routeType",unless = "#result == null")
    public List<ChargeMethod> getPayMethodList(Integer routeType,String tenantId) {
        if (routeType == null){
            routeType = 0;
        }
        return chargeMethodMapper.getPayMethodList(routeType,tenantId);
    }
}
